/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package player;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;
import java.util.regex.Pattern;
/**
 *
 * @author perrim
 */
public class Player {

        static Thread t;
        static int server_port = 80;
        static String fichier_temp = "/media";
        static SocketTCPClient manager;
        static String local_ip;
        static String ID;
        static String provider;
        
        
        
        
        
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException, SocketException, Exception {
       
    
        //local_ip = getLocalIP();

       // Lecture du fichier XML
        String fichier_xml = "demo.xml";
        
        if ( args.length > 0 )
            fichier_xml = args[0];
                        
        xml parser = new xml(fichier_xml);
        String ip_manager = parser.getManager();
         ID = parser.getId();
        String nom_media = parser.getName();
        fichier_temp = nom_media;
        
        
        // Connection au manager et recuperation IP fournisseur
         manager = new SocketTCPClient(ip_manager);
        provider = manager.getProvider(ID);     
        // Test provider ET demande un nouveau fournisseur SI down
        test_provider();
        
        
        String url_media = "http://" + provider + ":" + server_port + "/media/" + nom_media ;
        // Creation Player
        System.out.println("URL du media : " + url_media);
        //t = new Thread(new MediaPlayer(url_media, 0, 0, false));
        t = new Thread(new MediaPlayer(url_media, 0, 0, false, ID, ip_manager));
        t.start();
        
        
        // Sauvegarde en local du media        
        //Save(url_media);
        Download dl = new Download();
        dl.getFile(url_media);
        
        // Creation du web server
        WebServer serveur = new WebServer(fichier_temp, server_port);
        System.out.println(serveur);
                       
        
        // Enregistrement du Serveur aupres du manager
        manager.addProvider(ID, local_ip);
        
        
        
        // Attend que le thread mediaplayer soit detruit
        while ( t != null )       
        {
            Thread.sleep(200);
            System.out.print(".");
        }
        
        
        //System.out.println("Arret de l'application.");
        
        //manager.delProvider(ID, local_ip);
        //serveur.stop();
                
    }
    
    static private void test_provider()
    {
        
        boolean provider_ok = false;
        
        while (provider_ok == false) 
        {
            // Tester provider down        
            try 
            {
                Socket s = new Socket(provider, 80);
                s.close();
                provider_ok = true;
            } catch (Exception e) 
            {
                System.out.println("Error provider : " + provider + " down");
                manager.delProvider(ID, provider);
                provider = manager.getProvider(ID);
            }
        }
    }
     
    
    

}
